﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace l1cs.server
{
    public class BadNamesList
    {
        private static Logger _log = Logger.getLogger( typeof( BadNamesList ).FullName );

        private static BadNamesList _instance;

        private List<String> _nameList = new List<String>();

        public static BadNamesList getInstance()
        {
            if ( _instance == null )
            {
                _instance = new BadNamesList();
            }
            return _instance;
        }

        private BadNamesList()
        {
            FileStream fin = new FileStream( "data/badnames.txt", FileMode.Open );
            using ( StreamReader sr = new StreamReader( fin, Encoding.GetEncoding( "Shift_JIS" ) ) )
            {
                String line = null;
                while ( ( line = sr.ReadLine() ) != null )
                {
                    if ( line.Trim().Length == 0 || line.StartsWith( "#" ) )
                    {
                        continue;
                    }
                    String[] st = line.Split( ':' );
                    foreach ( String str in st )
                    {
                        _nameList.Add( str );
                    }
                }

                _log.config( "loaded " + _nameList.Count + " bad names" );
            }
        }

        public bool isBadName( String name )
        {
            foreach ( String badName in _nameList )
            {
                if ( string.Compare( name, badName,true ) == 0 )
                {
                    return true;
                }
            }
            return false;
        }

        public String[] getAllBadNames()
        {
            return _nameList.ToArray();
        }
    }
}
